package cn.petplatform.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @author LNL
 * @date 2021/4/25 15:30
 */
public class JWTUtils {

    private static final String SIGN = "@$%^QWEZXC$%^";


    public static String generateToken(Map<String,String> map) {
        JWTCreator.Builder builder = JWT.create();

        map.forEach((k,v) -> builder.withClaim(k,v));

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 7);

        String token = builder
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SIGN));
        return token;
    }

    public static void verifyToken(String token) {
        JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }
    public static DecodedJWT getUserInfo(String token) {
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }

}
